<?php
/**
 * QQAS - share your knowledge to the world!
 * 
 * @author		xiaochong0302 <xiaochong0302@gmail.com>
 * @copyright	Copyright (c) 2011, Koogua Studio.
 * @license		http://www.qqas.org/user_guide/license.html
 * @link		http://www.qqas.org
 * @since		Version 1.0
 * @filesource
 */
 
// ------------------------------------------------------------------------

/**
 * Blacklist
 *
 * @category	Admin
 */
class BlacklistAction extends AdminAction
{

	protected $Blacklist;
	
	
	public function _initialize()
    {
		parent::_initialize();
		$this->Blacklist = D('Blacklist');
	}
	
	
	//禁止用户
	public function banUser()
	{
		if ($this->isPost())
		{
			$type = $_POST['type'];
			if ('add' == $type)
			{
				if (!empty($_POST['username']))
				{
					$user = $this->_User->getUserByName($_POST['username']);
				}
				if (!empty($_POST['email']))
				{
					$user = $this->_User->getUserByEmail($_POST['email']);
				}
				if (empty($user))
				{
					$this->error(L('blacklist.user_not_existed'));
				}
				
				$map = array(
					'type'  => 'user_id',
					'value' => $user['user_id'],
			    );
				$count = $this->Blacklist->where($map)->count();
				if ($count >= 1)
				{
					$this->error(L('blacklist.banned_user_existed'));
				}
				//管理分组成员判断
				elseif (in_array($user['admin_group'], array(1,2,3)))
				{
				   	$this->error(L('blacklist.no_ban_admin_perms'));
				}
				else
				{
					$expiry = empty($_POST['ban_length']) ? 0 : time() + intval($_POST['ban_length']) * 86400;
					$data = array(
						'type'        => 'user_id',
						'value'       => $user['user_id'],
						'create_time' => time(),
						'expire_time' => $expiry,
						'author_id'   => $this->_admin['user_id'],
						'author_name' => $this->_admin['username'],
						'reason'      => trim($_POST['reason']),
					);
					$this->Blacklist->add($data);
					
					$userID = array($user['user_id']);
					$this->_User->banUserByID($userID, $_POST['ban_type'], $expiry);
					
					$this->success(L('blacklist.banned_user_added'));
				}
			}
			elseif ('remove' == $type)
			{
				$id = intval($_POST['id']);
				$result = $this->Blacklist->find($id);
				$userID = array($result['value']);
				
				$this->Blacklist->delete($id);
				$this->_User->banUserByID($userID, 'exclude', 0);
				$this->_User->autoUpgradeLevel($userID);
				
				$this->success(L('blacklist.banned_user_removed'));
			}
		}
		
		$prefix = C('DB_PREFIX');
		$join = "{$prefix}user ON {$prefix}blacklist.value={$prefix}user.user_id";
        $map = array('type' => 'user_id');
		$blacklist = $this->Blacklist->join($join)->where($map)->select();
		
		$this->assign('blacklist', $blacklist);
		$this->assign('banLengthOptions', L('day_options'));
		$this->display();
	}
	
	
	//不允许的用户名
	public function disallowUsername()
	{
		if ($this->isPost())
		{
			$type = $_POST['type'];
			if ('add' == $type)
			{
				$data = array(
					'type'        => 'username',
					'value'       => $_POST['username'],
					'create_time' => time(),
					'author_id'   => $this->_admin['user_id'],
					'author_name' => $this->_admin['username'],
				);
				$this->Blacklist->add($data);
				$this->Blacklist->cacheUsernameBlacklist();
				$this->success(L('blacklist.disallowed_username_added'));
			}
			elseif ('remove' == $type)
			{
				$id = intval($_POST['id']);
				if (!empty($id))
				{
					$this->Blacklist->delete($id);
					$this->Blacklist->cacheUsernameBlacklist();
					$this->success(L('blacklist.disallowed_username_removed'));
				}
			}
		}
		
		$map = array('type' => 'username');
		$order = array('value' => 'asc');
		$blacklist = $this->Blacklist->where($map)->order($order)->select();
		
		$this->assign('blacklist', $blacklist);
		$this->display();
	}
	
	
	//不允许的邮件地址
	public function disallowEmail()
	{
		if ($this->isPost())
		{
			$type = $_POST['type'];
			
			if ('add' == $type)
			{
				$email = $_POST['email'];
				if (!empty($email))
				{
					$data = array(
						'type'        => 'email',
						'value'       => $_POST['email'],
						'create_time' => time(),
						'author_id'   => $this->_admin['user_id'],
						'author_name' => $this->_admin['username'],
					);
					$this->Blacklist->add($data);
					$this->Blacklist->cacheEmailBlacklist();
					$this->success(L('blacklist.disallowed_email_added'));	
				}
			}
			elseif ('remove' == $type)
			{
				$id = intval($_POST['id']);
				if (!empty($id))
				{
					$this->Blacklist->delete($id);
					$this->Blacklist->cacheEmailBlacklist();
					$this->success(L('blacklist.disallowed_email_removed'));
				}
			}
		}
		
		$map = array('type' => 'email');
		$order = array('value' => 'asc');
		$blacklist = $this->Blacklist->where($map)->order($order)->select();
		
		$this->assign('blacklist', $blacklist);
		$this->display();
	}
	
	
	//禁止IP
	public function banIP()
	{
        if ($this->isPost())
		{
			$type = $_POST['type'];
			
			if ('add' == $type)
			{
				$ip = $_POST['ip'];
			    if (!empty($ip))
			    {
			    	$expiry = empty($_POST['ban_length']) ? 0 : time() + intval($_POST['ban_length']) * 86400;
	                $data = array(
	    			    'type'        => 'ip',
	    			    'value'       => trim($ip),
	    			    'create_time' => time(),
	    			    'expire_time' => $expiry,
	    			    'author_id'   => $this->_admin['user_id'],
	    			    'author_name' => $this->_admin['username'],
	    			    'reason'      => trim($_POST['reason']),
	    			);
	    			$this->Blacklist->add($data);
	    			$this->Blacklist->cacheIpBlacklist();
	    			$this->success(L('blacklist.banned_ip_added'));
			    }
			}
			elseif ('remove' == $type)
			{
				$id = intval($_POST['id']);
				if (!empty($id))
				{
					$this->Blacklist->delete($id);
					$this->Blacklist->cacheIpBlacklist();
					$this->success(L('blacklist.banned_ip_removed'));
				}
			}
		}

        $map = array('type' => 'ip');
        $order = array('value' => 'asc');
		$blacklist = $this->Blacklist->where($map)->order($order)->select();
		
		$this->assign('blacklist', $blacklist);
		$this->assign('banLengthOptions', L('day_options'));
		$this->display();
	}
	
}
// End  Class

/* End of file BlacklistAction.class.php */
/* Location: .App/Lib/Action/Admin/BlacklistAction.class.php */